package com.agu.mgt.sys.service;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.agu.utils.Query;
import com.agu.utils.Constant.MenuType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.agu.mgt.sys.dao.SysMenuDao;
import com.agu.mgt.sys.dao.SysUserDao;
import com.agu.entity.sys.SysMenuEntity;
import com.agu.mgt.sys.service.SysMenuService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("sysMenuService")
@Transactional(readOnly = true)
public class SysMenuService {
	@Autowired
	private SysMenuDao sysMenuDao;

	@Autowired
	private SysUserDao sysUserDao;

	public List<String> queryAllPerms(Long userId) {
		return sysUserDao.queryAllPerms(userId);
	}

	public List<Long> queryAllMenuId(Long userId) {
		return sysUserDao.queryAllMenuId(userId);
	}

	public List<SysMenuEntity> queryNotButtonList(int plat) {
		return sysMenuDao.queryNotButtonList(plat);
	}
	
	/**
	 * 获取所有菜单列表
	 */
	public List<SysMenuEntity> getAllMenuList(int plat) {
		return sysMenuDao.queryAllMenuList(plat);
	}

	public List<SysMenuEntity> getUserMenuList(Long userId) {
		// 系统管理员，拥有最高权限
		if (userId == 1) {
			return getAllMenuList(null);
		}

		// 用户菜单列表
		List<Long> menuIdList = sysUserDao.queryAllMenuId(userId);
		return getAllMenuList(menuIdList);
	}

	public Set<String> getUserPermissions(long userId) {
		List<String> permsList;

		// 系统管理员，拥有最高权限
		if (userId == 1) {
			List<SysMenuEntity> menuList = queryList(new HashMap<>());
			permsList = new ArrayList<>(menuList.size());
			for (SysMenuEntity menu : menuList) {
				permsList.add(menu.getPerms());
			}
		} else {
			permsList = sysUserDao.queryAllPerms(userId);
		}

		// 用户权限列表
		Set<String> permsSet = new HashSet<String>();
		permsSet.add("common");
		for (String perms : permsList) {
			if (StringUtils.isBlank(perms)) {
				continue;
			}
			permsSet.addAll(Arrays.asList(perms.trim().split(",")));
		}
		return permsSet;
	}

	public List<SysMenuEntity> queryListParentId(Long parentId, List<Long> menuIdList) {
		List<SysMenuEntity> menuList = sysMenuDao.queryListParentId(parentId);
		if (menuIdList == null) {
			return menuList;
		}

		List<SysMenuEntity> userMenuList = new ArrayList<>();
		for (SysMenuEntity menu : menuList) {
			if (menuIdList.contains(menu.getMenuId())) {
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}

	/**
	 * 获取所有菜单列表
	 */
	private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList) {
		// 查询根菜单列表
		List<SysMenuEntity> menuList = queryListParentId(0L, menuIdList);
		// 递归获取子菜单
		getMenuTreeList(menuList, menuIdList);

		return menuList;
	}

	/**
	 * 递归
	 */
	private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<Long> menuIdList) {
		List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();

		for (SysMenuEntity entity : menuList) {
			if (entity.getType() == MenuType.CATALOG.getValue()) {// 目录
				entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
			}
			subMenuList.add(entity);
		}

		return subMenuList;
	}

	public SysMenuEntity queryObject(Long menuId) {
		return sysMenuDao.queryObject(menuId);
	}

	public List<SysMenuEntity> queryList(Map<String, Object> map) {
		return sysMenuDao.queryList(map);
	}

	public Page<SysMenuEntity> queryPageList(Query query) {
		PageHelper.startPage(query.getPage(), query.getLimit());
		return sysMenuDao.queryPageList(query);
	}

	public int queryTotal(Map<String, Object> map) {
		return sysMenuDao.queryTotal(map);
	}

	@Transactional
	public void save(SysMenuEntity sysMenu) {
		sysMenuDao.save(sysMenu);
	}

	@Transactional
	public void update(SysMenuEntity sysMenu) {
		sysMenuDao.update(sysMenu);
	}

	@Transactional
	public void updateSort(ArrayList<HashMap> list) {

		int i = 0;
		for (Map map : list) {
			i++;

			if (map.containsKey("id")) {
				int id = (int) map.get("id");

				int sort = 0;
				if (map.containsKey("sort")) {
					sort = (int) map.get("sort");
				}

				if (i != sort) {
					Map pmap = new HashMap();
					pmap.put("sort", i);
					pmap.put("menuId", id);
					sysMenuDao.updateSort(pmap);
				}
			}

		}

	}

	@Transactional
	public void delete(Long menuId) {
		sysMenuDao.delete(menuId);
	}

	@Transactional
	public void deleteBatch(Long[] menuIds) {
		sysMenuDao.deleteBatch(menuIds);
	}
}
